# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=p11-kit
pkgname=('p11-kit' 'libp11-kit' 'libp11-kit-devel')
pkgver=0.23.9
pkgrel=1
pkgdesc="Library to work with PKCS#11 modules"
arch=('i686' 'x86_64')
url="https://p11-glue.freedesktop.org/p11-kit.html"
license=('BSD')
makedepends=('gettext-devel' 'libiconv-devel' 'libtasn1-devel' 'libffi-devel' 'glib2-devel' 'gtk-doc')
options=('!debug' 'strip' 'staticlibs')
source=(https://github.com/p11-glue/p11-kit/releases/download/${pkgver}/${pkgbase}-${pkgver}.tar.gz{,.sig}
        0001-allow-gtkdocize.all.patch
        0015-0.18.7-parallel-make.patch
        0016-0.18.7-win8-x64.patch
        0017-0.23.5-implicit-decls.patch
        0018-placeholder-debug.patch
        0100-msysize.patch)
validpgpkeys=('462225C3B46F34879FC8496CD605848ED7E69871')
sha256sums=('e1c1649c335107a8d33cf3762eb7f57b2d0681f0c7d8353627293a58d6b4db63'
            'SKIP'
            '195b2e8695f701caf545e2a468383ad29457febd9b1ee57de1986de04ad3c31c'
            'afc0fdcbf34327fadcc90e514cda72cce98c6813fb9dc6b9cb0fe233b07fc67c'
            'ae8b5639dd59d42596d5dab44d45799bf50cacbdbbf33d18d1afd51631a1b6d6'
            'e6afc75de6d0423bf61fc44b833c208a7578fb7e380a46be15353bb6d11f733c'
            'b3aa0093f50ed1c1ade7b7c0f1b6379517a047f8d4d20ed3c1f393812d16816d'
            '0e7476d160e8d00bfdbacc3cfc46d30f9ca2c040662bd78f21a373518d5cfe6c')

prepare() {
  cd "${srcdir}"/${pkgbase}-${pkgver}
  patch -p1 -i ${srcdir}/0001-allow-gtkdocize.all.patch
  patch -p1 -i ${srcdir}/0015-0.18.7-parallel-make.patch
  patch -p1 -i ${srcdir}/0016-0.18.7-win8-x64.patch
  patch -p1 -i ${srcdir}/0017-0.23.5-implicit-decls.patch
  #patch -p1 -i ${srcdir}/0018-placeholder-debug.patch
  patch -p1 -i ${srcdir}/0100-msysize.patch

  autoreconf -vfi
  gtkdocize
}

build() {
  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=( --enable-debug=yes )
  else
    extra_config+=( --enable-debug=no )
  fi

  [[ -d ${srcdir}/build ]] && rm -rf ${srcdir}/build
  mkdir -p "${srcdir}/build"
  cd "${srcdir}/build"

  PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/share/pkgconfig" \
  ../${pkgbase}-${pkgver}/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --with-trust-paths="/etc/pki/ca-trust/source:/usr/share/pki/ca-trust-source" \
    --disable-silent-rules \
    "${extra_config[@]}"

  make

  make DESTDIR="${srcdir}/dest" install
  install -Dm644 ../${pkgbase}-${pkgver}/COPYING ${srcdir}/dest/usr/share/licenses/${pkgbase}/COPYING
}

package_p11-kit() {
  depends=("libp11-kit=${pkgver}")

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/lib/pkcs11
  mkdir -p ${pkgdir}/usr/share/p11-kit/modules
  cp -f ${srcdir}/dest/usr/lib/pkcs11/*.dll ${pkgdir}/usr/lib/pkcs11/
  cp -f ${srcdir}/dest/usr/share/p11-kit/modules/*trust* ${pkgdir}/usr/share/p11-kit/modules/
  cp -rf ${srcdir}/dest/usr/libexec ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share/licenses ${pkgdir}/usr/share/
}

package_libp11-kit() {
  depends=('libffi' 'libintl' 'libtasn1' 'glib2')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libp11-kit-devel() {
  pkgdesc="Libp11-kit headers and libraries"
  groups=('development')
  depends=("libp11-kit=${pkgver}")

  mkdir -p ${pkgdir}/usr/lib/pkcs11
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/pkcs11/*.a ${pkgdir}/usr/lib/pkcs11/
}
